/**
 * SpringMVC restful 风格
 * 后台系统用户控制器，控制关于后台系统用户管理的一些业务逻辑操作
 * @author yanwl
 * @date 2015-01-27
 */
package com.chnbst.bstyun.controller.admin;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.DESUtil;
import com.chnbst.bstyun.model.pojo.AdminUser;
import com.chnbst.bstyun.model.pojo.StaticData;
import com.chnbst.bstyun.model.vo.AdminUserVo;
import com.chnbst.bstyun.service.AdminUserService;
import com.chnbst.bstyun.service.StaticDataService;

@Controller
@RequestMapping("/sysAdmin")
public class SysUserAdminController {
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private AdminUserService adminUserService;
	@Autowired
	private StaticDataService staticDataService;
	
	/**
	 * 后台系统用户管理页面
	 * @throws Exception
	 */
	@RequestMapping(value="/sysUserManager",method=RequestMethod.GET)
	public ModelAndView sysUserManage(HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sortField", "admin_num");
		map.put("sortOrder", "asc");
		List<AdminUserVo> list = adminUserService.selectAllAdminUsersVo(map);
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("list", list);
		mav.setViewName("admin/systemManager/sysUserManager");
		return mav;
	}
	
	/**
	 * 检测用户名是否存在
	 * @throws Exception
	 * @return boolean
	 */
	@RequestMapping(value="/sysUserManager/isExist",method=RequestMethod.POST)
	@ResponseBody
	public boolean isExist(String adminName,HttpServletRequest request)throws Exception{
		boolean isExist = false;
		AdminUser adminUser = adminUserService.selectByName(adminName);
		if(adminUser != null){
			isExist = true;
		}
		return isExist;
	}
	
	/**
	 * 查询后台用户对象
	 * @throws Exception
	 * @return boolean
	 */
	@RequestMapping(value="/sysUserManager/findSysUser",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findSysUser(Integer id,HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		AdminUser admin = adminUserService.selectById(id);
		map.put("admin", admin);
		return map;
	}
	
	/**
	 * 获取角色list对象
	 * @throws Exception
	 * @return boolean
	 */
	@RequestMapping(value="/sysUserManager/findRoles",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findRoles(HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<StaticData> roles = staticDataService.selectStaticDataByStaticDataCode("ADMINROLE");
		map.put("roles", roles);
		return map;
	}
	
	/**
	 * 新增或者修改后台用户
	 * @throws Exception
	 * @return boolean
	 */
	@RequestMapping(value="/operate/sysUserManager/addOrUpdateSysUser",method=RequestMethod.POST)
	@ResponseBody
	public boolean addOrUpdateSysUser(AdminUser adminUser,HttpServletRequest request)throws Exception{
		boolean isSuccess = false;
		adminUser.setState(1);
		if(adminUser.getAdminId() == null) {
			adminUser.setAdminPassword(DESUtil.encrypt(adminUser.getAdminPassword()));
			adminUser.setAddTime(new Date());
			isSuccess = adminUserService.insertAdminUser(adminUser);
		}else {
			AdminUser admin = adminUserService.selectById(adminUser.getAdminId());
			adminUser.setAddTime(admin.getAddTime());
			adminUser.setAdminPassword(admin.getAdminPassword());
			isSuccess = adminUserService.updateAdminUser(adminUser);
		}
		return isSuccess;
	}

	/**
	 * 重置后台用户密码或者删除用户
	 * @throws Exception
	 * @return boolean
	 */
	@RequestMapping(value="/operate/sysUserManager/delOrResetSysUser",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delOrResetSysUser(int flag,HttpServletRequest request)throws Exception{
		AdminUser adminUser = (AdminUser) request.getSession().getAttribute("adminUser");
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isSuccess = false;
		String[] adminIds = request.getParameter("array").split(",");
		for (String adminId : adminIds) {
			AdminUser admin = adminUserService.selectById(Integer.valueOf(adminId));
			if(adminUser.getAdminId().equals(admin.getAdminId())){
				//正在登陆用户不能删除
				map.put("message", "用户正在登陆中，请勿操作此用户！！");
			}else{
				if(flag == 0) {
					//删除用户
					if("MANAGER".equals(admin.getRoleId())){
						map.put("message", "此用户是管理员，不能删除！！");
					}else{
						admin.setState(0);
						isSuccess = adminUserService.updateAdminUser(admin);
					}
				}else {
					//重置后台用户密码
					admin.setAdminPassword(DESUtil.encrypt("11111111"));
					isSuccess = adminUserService.updateAdminUser(admin);
				}
			}
		}
		map.put("isSuccess", isSuccess);
		return map;
	}
}